#!/usr/local/bin/perl -w
use Data::Dumper;

=head1 NAME

EMS::LogIt - This is the EMS standard module for logging data.


=head1 SYNOPSIS

This is the EMS standard module for logging data.

=head1 METHODS

=over 4

=item * new - the default object constructor.  Construct with the
argument key/value array of: I<logfile=E<gt>filename>

=back

=over 4

=item * Death - A method to be used to return catastrophic failures.  
It's richer than the standard Perl die().  It takes an error number 
and a message as its arguments

=back

=over 4

=item * Add - a module (writted by stharms oddly enough) that uses Don
Johnsen's date algorithm (it's pretty that way) to maket an entry in a
logfile.  It should probably be aliased to a subroutine called 'add',
but that is neither here nor there at present.

=back

=cut

package EMS::LogIt;
use strict;


# Object Constructor
sub new {
  
   my $class = shift();
   my $self =  bless ({}, $class);
   my %defaults = (
 		  'message' => "",
 		  'logfile' => "/tmp/your_log_name_here",
                   @_
 		  );
   foreach (keys (%defaults)){
       $self->{$_}=$defaults{$_}
   }

  $self->{LOGFILE} = $self->{logfile};

  bless ($self);
  return $self;
}



sub add {
    my $self=shift;
    #print "ADD:  " . $self->{'logfile'} . "\n";
    open (LOGFILE,  ">>$self->{'logfile'}" ) or die("$!:  looking for =$self->{'logfile'}=");

	my ($logmessage) = @_;
	my @month = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );

    if(not  open( LOGFILE, ">>$self->{'logfile'}" ) ){
	open ( LOGFILE, ">>$self->{logfile}" );
    }

   	my ( $sec, $min, $hour, $mday, $mon, $year ) = localtime();
   	$year += 1900;

   	my $LogStr = sprintf( "%3s %2.2d %4s %2.2d:%2.2d:%2.2d %s %s \n",
   	   $month[$mon], $mday, $year, $hour, $min, $sec, "[$$]", $logmessage);

        # this is a sanity check to make sure that the $Logstr file handle is open
        # if not we will simply not log whatever.


    if (-e LOGFILE){
        print LOGFILE $LogStr;
    }else{

	# if it is closed, which it shouldn't be, let's log the data
	# for future purposes.

	open (X, ">/tmp/DEBUG");
	print X "The file handle was not open at $LogStr\n";
	foreach (%{+ $self}){
	    print X $$self{$_};
	}

    }
    close LOGFILE;

}


# Subroutine to get current date/time
sub date {
  my $self = shift;
  my $now = system("date +%D_%H:%M:%S");
  return $now;

}

# Subroutine to get logfile location
sub logfile {
  my $self = shift;
  if (@_) {
    $self->{logfile} = shift }
  return $self->{'logfile'};
}

=pod

=over 4

=item * subdebug() / subdie() / subwarn() - If you need to log data
into a file for debugging it will simply append to the file.  This is
not much different than add() but was borrowed from the
Mirapoint::Admin module.  subdie() and subwarn() have the additional
element that if you use them they will either warn() or die() upon
printing to.

=back

=cut



sub subdebug
{
    my $self=shift;
    my $LOGFILE= $self->{LOGFILE};

	my $msg = join('', @_);

	$msg =~ s/[\r\n]*$//;
	open(SF, ">>$LOGFILE") || die "Cannot open $LOGFILE: $!\n";
	print SF "debug: $msg\n";
	close(SF);
}


sub subdie
{
    my $self=shift;
    my $LOGFILE= $self->{LOGFILE};

    $LOGFILE=$self->{CONFIG}{LOGFILE};

	if (defined $LOGFILE) {
		open(F, ">>$LOGFILE");
		print F @_, "\n";
		close(F);
	}
	print STDERR "die: ", @_, "\n";
	exit(1);
}

sub Death {
        my ($self, $mailerror, $message) = @_;
        # make sure we log something and return appropriate error code to sendmail
        # so it will bounce a message back.
	    my @month = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
   	    my ( $sec, $min, $hour, $mday, $mon, $year ) = localtime();
        $year += 1900;

        my $process = $$;
     	my $LogStr = sprintf( "%3s %2.2d %4s %2.2d:%2.2d:%2.2d %s %s \n",
   	    $month[$mon],$mday,$year,$hour, $min, $sec,qq{[$process]}, $message );
        $self->add("[DEATH]: $LogStr");
        print STDERR  "EMS:LogIt Death Encountered:  $LogStr";
        exit($mailerror);
}

sub subwarn
{
    my $self=shift;
    my $LOGFILE= $self->{LOGFILE};

     $self=shift;
     $LOGFILE=$self->{CONFIG}{LOGFILE};

	if (defined $LOGFILE) {
		open(F, ">>$LOGFILE");
		print F @_, "\n";
		close(F);
	}
	warn (@_);
}


return 1;
__END__

=head1 CREATED / LAST MODIFIED / CHANGELOG

B<First Entry:>
Created POD documentation, cleaned up module, 4/11/2003, Steven Harms

=head1 AUTHOR

Originally written by John Wheeler.  Pretty date algorithm by
Don Johnsen.  

Module cleaned up and integrated into nails by Steven G. Harms 
I<stharms@cisco.com>, 3/2003




